jQuery(document).ready(function() {
	var currentOrgcode = '';
	$.ajax({
		async : false,
		url : projectPath + "user/getCurrentUser.do",
		data : {},
		dataType : "json",
		success : function(data) {
			$('#org').val(data.orgname);
			$('#org').attr('data', data.orgcode);
			currentOrgcode = data.orgcode;
		}
	});
	
	initTable();
	
	App.setPage("query");  //Set current page
	App.init(); //Initialise plugins and elements
	

	bindDict("itemtypecode", "DM_XKXMLX", null);
	
	//列表查询事件
	$('#search').click(function(){
		$('#table').bootstrapTable(('refresh'));  // 很重要的一步，刷新url！
	});
	
	//绑定机构下拉列表
	bindOrg2('org', currentOrgcode, "请选择受理机构");
	
	//保存数据事件
	$('#save').click(function(){
		$("#ifModal")[0].contentWindow.save();
	});
	
	//清空表单数据事件
	$("#modalDict").on("hidden.bs.modal", function() {
		$('#tableDictDetail').find('tbody').find('tr').not('.no-records-found').remove();	
		if($('#' + tableID).find('tbody').children('tr').length == 0){
			var trStr = '<tr class="no-records-found">	'+
		    '   <td colspan="7" style="font-size:14px;">			'+
			'		单击底部“新增行”增加字典！		'+
			'	</td>						'+
			'</tr>';
			
			$('#' + tableID).find('tbody').append(trStr);
		}
		
		$(this).find('[type="text"]').val('');
		$(this).find('[type="checkbox"]').prop("checked",true);     
	});
	
	$('#modal').on("hidden.bs.modal", function() {  
		$('#modalTile').html("业务详细");
	});
});		



function initTable(){		
	$('#table').bootstrapTable({
		method: 'post',
	    url: basePath() + 'business/list.do',
	    pagination: true, //分页
	    dataType: "json",
        contentType: "application/json",
        pageList: [10, 20, 50, 100],  //可供选择的每页的行数（*）
        striped: true,      //是否显示行间隔色
        sidePagination: "server",
        ////查询参数,每次调用是会带上这个参数，可自定义                         
        queryParams:function(params) {
            return {
                	pageNum: params.offset / params.limit + 1,
                	pageSize: params.limit,
                	orgcode: $('#org').attr('data'),
                	itemtypecode: $('#itemtypecode').val(),
                	itemname: $('#itemname').val(),
                	applyno: $('#applyno').val(),
                	startapplytime: $('#startapplytime').val(),
					endapplytime: $('#endapplytime').val()
                };
        },
	    columns: [{
            field : 'number',  
            title : '序号',  
            align : 'center',
            width : '50px',
            formatter : function(value, row, index) {  
// 		                return index + 1;  
                var page = $('#table').bootstrapTable("getPage");  
                return page.pageSize * (page.pageNumber - 1) + index + 1;  
            }  
        }, {
	        field: 'applyno',
	        title: '申请单号',
	            formatter : function(value, row, index) {  
			        return '<a onclick="showDetail(\''+ value +'\', \''+ row.auditusername +'\', \''+ row.instanceid +'\')">' + value + '</a>';  
			    } 
	    }, {
	        field: 'itemname',
	        title: '项目名称'
	    }, {
	        field: 'itemtype',
	        title: '项目类型',
            formatter : function(value, row, index) {  
		        return row.itemtypecode + '-' + value;  
		    } 
	    }, {
	        field: 'applytime',
	        title: '申请时间',
            formatter : function(value, row, index) {  
		        return formatDate(value, 'yyyy-mm-dd hh:mi');  
		    } 
	    }, {
            field: 'orgname',
            title: '受理机构',
	    }, {
            field: 'nodename',
            title: '状态',
            formatter : function(value, row, index) {
            	var rValue = value;
            	if(value == '办结' || value == '发证' )
            		rValue = '<span style="color:green;font-weight: bold;">'+ value +'</span>';
            	else if(value.indexOf('不予') >= 0)
            		rValue = '<span style="color:red;font-weight: bold;">'+ value +'</span>';
            	else
            		rValue = '<span style="color:blue;font-weight: bold;">'+ value +'</span>';

            	return rValue;  
		    } 
	    }, {
            field: 'operate',
            title: '操作',
            align: 'center',
            events: operateEvents,
            formatter: operateFormatter
        }]
	});
}

function operateFormatter(value, row, index) {
    if(row.nodename == '办结' || row.nodename == '发证'){
    	if(row.serviceattitude == 0){
		    return [
		        	'<button type="button" class="comment btn btn-link">未评价</button>'
		    ].join('');
    	}
    	else{
		    return [
		        	'<button type="button" class="comment btn btn-link">查看评价</button>'
		    ].join('');
    	}
    }
//    else{
//	    return [
//	        	'<button type="button" class="delete btn btn-link">删除</button>'
//	    ].join('');
//    }
}

window.operateEvents = {
  'click .delete': function (e, value, row, index) {		//列表操作栏【删除】

		window.wxc.xcConfirm('是否<strong>删除</strong>申报单吗?<br /><span style="color:red;">['+ row.applyno +']</span>', window.wxc.xcConfirm.typeEnum.info,{
			onOk:function(v){
				
				$.ajax({ 
					url: projectPath + "business/delete.do",
					data: {"applyno": row.applyno},
					dataType: "json",
					success: function(data){	            						
						if(data.result == 1){
				        	$('#table').bootstrapTable(('refresh'));  // 很重要的一步，刷新url！
						}
			      	}
				});
			}
		});	
  },
  'click .comment': function (e, value, row, index) {		//列表操作栏【评价】
	  $('#applyno1').html(row.applyno);
	  $('#itemname1').html(row.itemname);
	  $('#itemtype').html(row.itemtype);
	  $('#orgname').html(row.orgname);
	  $('#applyid').val(row.applyid);
	  
	  if(row.serviceattitude == 0){
		  $("#serviceattitude").val(0);
		  $("#handlespeed").val(0);
	  }
	  else{
		  $("#serviceattitude").val(row.serviceattitude);
		  $("#handlespeed").val(row.handlespeed);
	  }
	  $("#serviceattitude").rating('create');
	  $("#handlespeed").rating('create');
	  
	  $('#modalComment').modal('show');
}
};

function showDetail(applyno, username, instanceid){
	if('' != instanceid){
		var param = {};
		param["instanceid"] = instanceid;
		
		$.ajax({ 
			method: 'post',
			url: projectPath + "business/getapproveusername.do",
			data: JSON.stringify(param),
			dataType: "json",
			contentType: "application/json",
			success: function(data){
				if(undefined != data.datas){
					username = '<span style="color: red">（当前人员：'+ data.datas +'）</span>';
					$('#modalTile').html("业务详细　-　" + applyno + username);
				}
			}
		});
	}
//	if(username != "")
//		username = '<span style="color: red">（当前人员：'+ username +'）</span>';
		
	setModalSize();
	
	$('#ifModal').attr("src", "detail.html?applyno=" + applyno+"&instanceid="+instanceid);
	
	$("#modal").modal('show');
}